<template>
  <a-card class="cardShadow">
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="工业">
          <a-input v-decorator="['totalEnergyConsumption', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入工业的值" suffix="" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="批发和零售业">
          <a-input v-decorator="['energyConsumptionIntensity', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入批发和零售业的值" suffix="" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="卫生和社会工作">
          <a-input v-decorator="['totalCarbonEmissions', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入卫生和社会工作的值" suffix="" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="教育">
          <a-input v-decorator="['totalCarbonEmissions', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入教育的值" suffix="" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="交通运输、仓储和邮政业">
          <a-input v-decorator="['totalCarbonEmissions', {
            rules: [
              {
                required: true,
                message: '仅能输入数字和两位小数，不能为空或为负数！',
                pattern: /^[0-9]+\.?[0-9]{0,2}$/
              },
            ],
          }]" placeholder="请输入交通运输、仓储和邮政业的值" suffix="" />
        </a-form-item>
      </a-form>
      <a-button :loading="confirmLoading" class="button" icon="upload" type="primary" @click="handleOk()">提交</a-button>
    </a-spin>
  </a-card>
</template>

<script>
import pick from 'lodash.pick'
import { getAction, postAction } from '@api/manage'
import Vue from 'vue'
import { LACK } from '@/store/mutation-types'

export default {
  name: 'keyIndustryRanking',
  data() {
    return {
      confirmLoading: false,
      title: '',
      visible: false,
      form: this.$form.createForm(this),
      dialogStyle: {
        top: '200px'
        // right:"20px",
      },
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      // url: {
      //   add: '/carbon/carbonOverviewYearData/add',
      //   edit: '/carbon/carbonOverviewYearData/edit'
      // }
    }
  },
  created() {
  },
  mounted() {
    this.getData()
  },
  methods: {

    getData() {
      let year = String(new Date().getFullYear())
      getAction('//carbon/enterpriseFileConsumptionRankList/list', {}).then((res) => {
        if (res.success) {
          console.log(res)
          this.model = res.result.records[0]
          this.$nextTick(() => {
            this.form.setFieldsValue(pick(
              this.model,
              'year',
              'totalEnergyConsumption',
              'totalEnergyConsumptionYoy',
              'totalCarbonEmissions',
              'totalCarbonEmissionsYoy',
              'energyConsumptionIntensity',
              'energyConsumptionIntensityYoy',
              'carbonEmissionIntensity',
              'carbonEmissionIntensityYoy'))
          })
        }
      })
    },
    close() {
      this.$emit('close')
      this.visible = false
    },
    handleCancel() {
      this.close()
    },
    handleOk() {
      let that = this
      this.form.validateFields((err, values) => {
        if (!err) {
          let httpurl = ''
          this.confirmLoading = true
          console.log(this.model)
          if (!this.model) {
            httpurl += this.url.add
          } else {
            httpurl += this.url.edit
          }
          console.log(httpurl)
          let formData = Object.assign(values)
          formData.areaCode = '021'
          if (!formData.year) {
            let year = String(new Date().getFullYear())
            formData.year = year
          }
          // postAction(httpurl, formData).then(res => {
          //   console.log(res)
          //   if (res.success) {
          //     that.$message.success(res.message)
          //     that.$emit('refresh')
          //   } else {
          //     that.$message.error(res.message)
          //   }
          // }).finally(() => {
          //   that.confirmLoading = false
          // })
        }
      })
    }
  }
}
</script>

<style lang="stylus" scoped>
.button {
  margin-left: 50%;
  transform: translateX(-50%);
}
</style>